﻿/*
 * SptlDbParameterCollection
 * 
 * initial implementation : 2.12.2008
 *
 * Copyright (c) Adam Sida, asida@centrum.cz.
 * All rights reserved.
 * 
 * for license see Petra.Gis.License.txt
 */
using System;
using System.Data.Common;
using System.Collections.Generic;


namespace Petra.Gis.DataServices
{
  public abstract class SptlDbParameterCollection : DbParameterCollection
  {

    //protected Dictionary<string, object> xparams;
    public XCommandParameters xparams;


    protected SptlDbParameterCollection()
    {
      //xparams = new Dictionary<string, object>();
      xparams = new XCommandParameters();
    }




    public override int Add(object value)
    {

      if (!(value is SptlDbParameter))
        throw new ArgumentException("invalid input of type " + value.GetType().ToString());

      xparams.Add(((SptlDbParameter)value).ParameterName, value);
      return xparams.Count - 1;
    }




    public override void Clear()
    {
      xparams.Clear();
    }



    public override int IndexOf(object value)
    {
      int counter = 0, result = -1;

      //if (xparams.ContainsValue(value))
      foreach (KeyValuePair<string, object> p in xparams)
      {
        if (p.Equals(value))
          result = counter;

        counter++;
      }
      return result;
    }




    public override int Count
    {
      get { return xparams.Count; }
    }




    public override bool Contains(string value)
    {
      return xparams.ContainsKey(value);
    }




    public override bool Contains(object value)
    {
      return xparams.ContainsValue(value);
    }



    public override System.Collections.IEnumerator GetEnumerator()
    {
      return xparams.GetEnumerator();
    }




    protected override DbParameter GetParameter(int index)
    {
      int counter = 0;
      SptlDbParameter result = null;
      foreach (KeyValuePair<string, object> p in xparams)
      {
        if (index == counter)
          result = (SptlDbParameter)p.Value;
      }
      return result;
    }




    protected override DbParameter GetParameter(string parameterName)
    {
      return (SptlDbParameter)xparams[parameterName];
    }




    public override void Insert(int index, object value)
    {
      if (!(value is SptlDbParameter))
        throw new ArgumentException("invalid parameter " + value.GetType().ToString());

      this.SetParameter(((SptlDbParameter)value).ParameterName, ((SptlDbParameter)value));
    }




    public override void AddRange(Array values)
    {
      throw new NotImplementedException();
    }




    public override void CopyTo(Array array, int index)
    {
      throw new NotImplementedException();
    }



    public override void Remove(object value)
    {
      xparams.Remove(((SptlDbParameter)value).ParameterName);
    }




    protected override void SetParameter(int index, DbParameter value)
    {
      this.SetParameter(value.ParameterName, value);
    }



    protected override void SetParameter(string parameterName, DbParameter value)
    {
      if (parameterName != value.ParameterName)
        throw new ArgumentException("parameterName != value.ParameterName");

      xparams.Add(parameterName, value);
    }





    public override void RemoveAt(string parameterName)
    {
      xparams.Remove(parameterName);
    }




    public override void RemoveAt(int index)
    {
      this.RemoveAt(this.GetParameter(index).ParameterName);
    }




    public override int IndexOf(string parameterName)
    {
      int counter = 0, result = -1;
      foreach (KeyValuePair<string, object> p in xparams)
      {
        if (parameterName == p.Key)
          result = counter;

        counter++;
      }
      return result;
    }




    public override bool IsReadOnly
    {
      get { return false; }
    }




    public override bool IsFixedSize
    {
      get { return false; }
    }




    public override bool IsSynchronized
    {
      get { return false; }
    }




    public override object SyncRoot
    {
      get { return null; }
    }


  }
}
